﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;
using AviagePortal.Entity;

namespace AviagePortal.Layouts.AviagePortal
{
    /// <summary>
    ///  http://msstoresp12013/sites/ap/_layouts/15/AviagePortal/SearchProduct.ashx?searchText=Development&language=English&deviceType=PC&pageInfo=3
    /// </summary>
    public partial class SearchProduct : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {            
            System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            context.Response.ContentType = "application/json";          

            string searchText = context.Request.QueryString["searchText"];
            string language = context.Request.QueryString["language"];
            string deviceType = context.Request.QueryString["deviceType"];
            string pageInfo = context.Request.QueryString["pageInfo"];
            SearchProductResult result = new SearchProductResult();
            if (string.IsNullOrWhiteSpace(searchText))
            {
                result.ErrorMessage = "Parameter searchText can't be null";
            }
            else if (string.IsNullOrWhiteSpace(language))
            {
                result.ErrorMessage = "Parameter language can't be null";
            }
            else if (string.IsNullOrWhiteSpace(deviceType))
            {
                result.ErrorMessage = "Parameter deviceType can't be null";
            }
            else
            {
                // Use caml query the news.
                // Search the follow columns: Title, SubTitle,CompanyAboutTitle,Content,CompanyAboutContent 
                // NewsDetail
                SPList newsList = SPContext.Current.Web.Lists["NewsDetail"];
                SPQuery newsQuery = new SPQuery();
                string caml = @"<Where>
                          <And>
                            <And>     
                                <Or>
                                  <Or>
                                    <Or>
                                    <Or>
                                      <Contains>
                                        <FieldRef Name='Title'/>
                                        <Value Type='Text' >{0}</Value>
                                      </Contains>
                                      <Contains>
                                        <FieldRef Name='SubTitle'/>
                                        <Value Type='Text' >{0}</Value>
                                      </Contains>
                                    </Or>
                                    <Contains>
                                      <FieldRef Name='CompanyAboutTitle'/>
                                      <Value Type='Text' >{0}</Value>
                                    </Contains>
                                  </Or>
                                  <Contains>
                                    <FieldRef Name='Content'/>
                                    <Value Type='Notes' >{0}</Value>
                                  </Contains>
                                </Or>  
                                    <FieldRef Name='ShortDescription'/>
                                    <Value Type='Notes' >{0}</Value>
                                  </Contains>
                                </Or>       
                              <Eq>
                                <FieldRef Name='DeviceType'/>
                                <Value Type='Choice' >{2}</Value>
                              </Eq>
                            </And>
                            <Eq>
                              <FieldRef Name='Language'/>
                              <Value Type='Choice' >{1}</Value>
                            </Eq>
                          </And>
                        </Where>";
                caml = string.Format(caml, searchText.Trim(), language.Trim(), deviceType.Trim());
                newsQuery.Query = caml;
                newsQuery.RowLimit = 4;
                if (!string.IsNullOrWhiteSpace(pageInfo) && pageInfo != "null")
                {
                    newsQuery.ListItemCollectionPosition = new SPListItemCollectionPosition(pageInfo);
                }

                SPListItemCollection items = newsList.GetItems(newsQuery);

                if (items.ListItemCollectionPosition != null)
                {
                    result.PageInfo = items.ListItemCollectionPosition.PagingInfo;
                }

                foreach (SPListItem item in items)
                {
                    NewsDetailItem newsDetailItem = new NewsDetailItem();
                    newsDetailItem.Title = "" + item["Title"];
                    newsDetailItem.ID = item.ID.ToString();
                    newsDetailItem.SubTitle = "" + item["SubTitle"];
                    newsDetailItem.Order = item["Order0"] == null ? 99999 : int.Parse("" + item["Order0"]);
                    newsDetailItem.Date = item["Date"] == null ? null : (DateTime?)item["Date"];
                    newsDetailItem.Content = "" + item["Content"];
                    newsDetailItem.CompanyAboutTitle = "" + item["CompanyAboutTitle"];
                    newsDetailItem.CompanyAboutContent = "" + item["CompanyAboutContent"];
                    newsDetailItem.Picture = "" + item["Picture"];
                    newsDetailItem.ShortDescription = "" + item["ShortDescription"]; 
                    if ("" + item["Language"] == "English")
                    {
                        newsDetailItem.URL = "/pages/NewsDetail.aspx?newsid=" + newsDetailItem.ID;
                    }
                    else if ("" + item["Language"] == "Chinese")
                    {
                        newsDetailItem.URL = "/pages/NewsDetail_cn.aspx?newsid=" + newsDetailItem.ID;
                    }

                    result.NewsItems.Add(newsDetailItem);
                }
            }

            context.Response.Write(jsonSerializer.Serialize(result));
        }
    }
}
